package com.byl.sdk.video;

import org.bytedeco.javacv.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * Created by Administrator on 2019/12/17.
 */
public class VideoUtils {
    final static int WIDTH=520;
    final static int HEIGHT=340;

    /**
     * 抓取視頻首貞圖像
     * @param request
     * @param path
     * @return
     */
   public static String grap(HttpServletRequest request,String path){
        ServletContext sc = request.getSession().getServletContext();
        String root = sc.getRealPath("/").replaceAll("\\\\","/");
        System.out.println("path>>>>>>>>>>>"+root+path);
        File videoFile=new File(root+path);
        FFmpegFrameGrabber ffmpegFrameGrabber = new FFmpegFrameGrabber(videoFile);
        try {
            ffmpegFrameGrabber.start();
        } catch (FrameGrabber.Exception e) {
            e.printStackTrace();
        }
        org.bytedeco.javacv.Frame frame = null;
        //截取中间图像
            int i = 0;
            try {
                int length = ffmpegFrameGrabber.getLengthInFrames();
                int middleFrame = (int) length/2;
                System.out.println("middleFrame>>>>>>>>>>>>>>"+middleFrame);
                if(middleFrame>1440)
                    middleFrame=1440;
                while (i < length) {
                    frame = ffmpegFrameGrabber.grabFrame();
                    if ((i >= middleFrame) &&(frame.image != null)) {
                        break;
                    }
                    frame=null;
                    i++;
                }
            } catch (FrameGrabber.Exception e) {
                e.printStackTrace();
            }
        Java2DFrameConverter frameConverter = new Java2DFrameConverter();
        BufferedImage bufferedImage = frameConverter.getBufferedImage(frame);
        BufferedImage bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_3BYTE_BGR);
        bi.getGraphics().drawImage(bufferedImage.getScaledInstance(WIDTH, HEIGHT, Image.SCALE_SMOOTH),
                0, 0, null);
        path=path.substring(0,path.lastIndexOf("."))+".jpg";
        System.out.println("圖片>>>>>>>>>"+path);
        File targetFile =new File(root+path);
        try {
            ImageIO.write(bi, "jpg", targetFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            ffmpegFrameGrabber.stop();
        } catch (FrameGrabber.Exception e) {
            e.printStackTrace();
        }
        return path;
    }


    /**
     * 获取视频时长，单位为秒
     *
     * @param video 源视频文件
     * @return 时长（s）
     */
     static long getVideoDuration(File video) {
        long duration = 0L;
        FFmpegFrameGrabber ff = new FFmpegFrameGrabber(video);
        try {
            ff.start();
            duration = ff.getLengthInTime() / (1000 * 1000);
            ff.stop();
        } catch (FrameGrabber.Exception e) {
            e.printStackTrace();
        }
        return duration;
    }
}
